﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class TicketDAO : BaseService
    {
        public bool IsExist(string ticketNo)
        {
            string hql = "from Ticket where TicketNumber like '" + ticketNo + "'";
            IList<Ticket> tlist =  FindList<Ticket>(hql,null,null,null);
            if (tlist != null && tlist.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool IsExistByOrderId(string orderId)
        {
            string hql = "from Ticket where  OrderId like '" + orderId + "'";
            IList<Ticket> tlist = FindList<Ticket>(hql, null, null, null);
            if (tlist != null && tlist.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #region 根据Groupid查询
        /// <summary>
        ///  根据Groupid查询团卷
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IList<Ticket> GetTicketByGroupid(int groupid, int page, int pagesize)
        {
            try
            {
                string hql = "from Ticket where GroupId=" + groupid ;
                return FindList<Ticket>(hql, null, (page - 1) * pagesize, pagesize);
                
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 根据Groupid查询团卷总数
        /// </summary>
        /// <param name="groupid"></param>
        /// <returns></returns>
        public int GetTickeByGroupid(int groupid)
        {
            string hql = "select count(*) from Ticket where GroupId=" + groupid;

            return Convert.ToInt32(GetValue(hql));
        }
        #endregion

        #region 根据state返回总数
        /// <summary>
        /// 根据state返回总数
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetContByState(int state,int groupid)
        {
            string hql = "select count(*) from Ticket where State=" + state+" and GroupId="+groupid ;
          // 
            return Convert.ToInt32(GetValue(hql));

        }
        #endregion

        #region
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int UpdateState(string ids,int state)
        {
            int m = 0;
            string hql = "Update from Ticket set state=" + state + " where Id in ("+ids+")";
            try
            {
                 m = DBHelper.GetISession.CreateQuery(hql).ExecuteUpdate();
                return m;
            }
            catch
            {
                return m;
            }

        }
        #endregion

        public IList<Ticket> GetTicket(int tj,int userId,int page,int pagesize)
        {
            string hql = " from Ticket where 1=1";
            //if (tj == 2)
            //{
            //    hql += " and State>0";
            //}
            //else
            if (tj <2)
            {
                hql += " and State="+tj;
            }
            hql += " and UserId =" + userId + " order by Id desc";

            return FindList<Ticket>(hql,null,(page-1)*pagesize,pagesize);
        }
        public int GetTicket(int tj, int userId)
        {
            string hql = " select count(*) from Ticket where 1=1";
            //if (tj == 2)
            //{
            //    hql += " and State>0";
            //}
            //else
            if (tj < 2)
            {
                hql += " and State=" + tj;
            }
            hql += " and UserId =" + userId ;

            return Convert.ToInt32(GetValue(hql));
        }
        /// <summary>
        ///  根据Groupid查询团卷,用户导出excel
        /// </summary>
        /// <param name="groupid"></param>
        /// <returns></returns>
        public IList<Ticket> GetTicketByGroupid(int groupid)
        {
            try
            {
                string hql = "from Ticket where GroupId=" + groupid;
                return FindList<Ticket>(hql, null, null, null);
               
            }
            catch
            {
                return null;
            }
            
        }
    }
}
